package com.spring.demo.init.mail;

import java.io.IOException;
import java.text.ParseException;
import java.util.Properties;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import com.spring.demo.core.job.QuartzJob;
import com.spring.demo.core.job.QuartzJobSchedulingListener;
import com.spring.demo.core.job.SpringQuartzJobFactory;
import com.spring.demo.core.service.IMailService;
import com.spring.demo.core.service.impl.MailService;

@Configurable
@ComponentScan(basePackages = "com.spring.demo.core", includeFilters = { @Filter(value = QuartzJob.class) }, useDefaultFilters = false)
@PropertySource({ "classpath:mail/mail.properties" })
public class MailConfig {

	@Autowired
	private Environment env;

	@Bean
	public IMailService mailService() {

		return new MailService();
	}

	@Bean
	public VelocityEngine velocityEngine() {
		VelocityEngine velocityEngine = new VelocityEngine();
		// 从classpath加载模版文件
		velocityEngine.setProperty("resource.loader", "class");
		velocityEngine
				.setProperty("class.resource.loader.class",
						"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
		return velocityEngine;
	}

	/**
	 * 方法springQuartzJobFactory()/quartzJobSchedulingListener()，装配任务、触发器、调度器等
	 * 
	 * @return
	 */
	@Bean
	public SpringQuartzJobFactory springQuartzJobFactory() {
		return new SpringQuartzJobFactory();
	}

	@Bean
	public QuartzJobSchedulingListener quartzJobSchedulingListener() {
		return new QuartzJobSchedulingListener();
	}

	/**
	 * 定时任务.<br >
	 * 定时任务会一直在跑，测试环境注释掉
	 * 
	 * @return
	 * @throws ParseException
	 * @throws IOException
	 */
	@Bean
	@Autowired
	public SchedulerFactoryBean schedulerFactoryBean(
			SpringQuartzJobFactory jobFactory) throws ParseException,
			IOException {
		SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
		factoryBean.setJobFactory(jobFactory);

		return factoryBean;
	}

	@Bean
	public JavaMailSenderImpl mailSender() {
		Properties mailProperties = new Properties();
		mailProperties.setProperty("mail.smtp.auth",
				env.getRequiredProperty("mail.smtp.auth"));
		mailProperties.setProperty("mail.smtp.starttls.enable",
				env.getRequiredProperty("mail.smtp.starttls.enable"));

		String host = env.getRequiredProperty("mail.host");
		int port = env.getRequiredProperty("mail.port", Integer.class);
		String protocol = env.getRequiredProperty("mail.transport.protocol");
		String username = env.getRequiredProperty("mail.username");
		String password = env.getRequiredProperty("mail.password");
		JavaMailSenderImpl mailSenderImpl = new JavaMailSenderImpl();
		mailSenderImpl.setHost(host);
		mailSenderImpl.setPort(port);
		mailSenderImpl.setProtocol(protocol);
		mailSenderImpl.setUsername(username);
		mailSenderImpl.setPassword(password);
		mailSenderImpl.setJavaMailProperties(mailProperties);

		return mailSenderImpl;
	}

	@Bean
	public SimpleMailMessage alertMailMessage() {
		String username = env.getRequiredProperty("mail.username");
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		mailMessage.setFrom(username);
		mailMessage.setTo(username);
		mailMessage.setSubject("系统异常邮件");

		return mailMessage;
	}
}
